#include "MainWindow.h"
#include <QtGui/QFileDialog>
#include <string>
#include <assert.h>
#include "colorswatch.h"
#include <QTextEdit>
#include <QFileInfo>
#include "PaintCanvas.h"
#include "VideoCanvas.h"
#include "CamCaptureCavas.h"
#include <iostream>
OCEANUI::CMainWindow::CMainWindow(void):
	pCurrentImage_(NULL)
{
	resize(QSize(800,600));
	createDockWindows_();
	createActions();
	createToolBars();
	createMenus();
	for (int i = 0; i<3; i++)
	{
		CentralCanvasGroup[i] = NULL;
	}
	setCurrentCentralCanvas_(PaintCanvas);
	pPanelManager = new CDockPanelManager(dock,this);
}
OCEANUI::CMainWindow::~CMainWindow(void)
{
}

void OCEANUI::CMainWindow::createActions()
{
	lineAct = new QAction(QIcon(":/images/line.png"), tr("&Line"), this);
	lineAct->setCheckable(true);

	polylineAct = new QAction(QIcon(":/images/polyline.png"), tr("&polyline"), this);
	polylineAct->setCheckable(true);

	rectangleAct = new QAction(QIcon(":/images/rectangle.png"), tr("&rectangle"), this);
	rectangleAct->setCheckable(true);

	polygonAct = new QAction(QIcon(":/images/polygon.png"), tr("&polygon"), this);
	polygonAct->setCheckable(true);

	arcAct = new QAction(QIcon(":/images/arc.png"), tr("&Arc"), this);
	arcAct->setCheckable(true);

	pieAct = new QAction(QIcon(":/images/pie.png"), tr("&Pie"), this);
	pieAct->setCheckable(true);

	chordAct = new QAction(QIcon(":/images/chord.png"), tr("&Chord"), this);
	chordAct->setCheckable(true);

	ellipseAct = new QAction(QIcon(":/images/ellipse.png"), tr("&Ellipse"), this);
	ellipseAct->setCheckable(true);
	ellipseAct->setChecked(true);

	textAct = new QAction(QIcon(":/images/text.png"), tr("&Text"), this);
	textAct->setCheckable(true);

	openAct = new QAction(QIcon(":/images/open.png"), tr("&Open"), this);
	openAct->setCheckable(true);
    connect(openAct,SIGNAL(triggered()), this, SLOT(OnFileOpen()));

	openCamAct = new QAction(QIcon(":/images/open.png"), tr("&Open"), this);
	openCamAct->setCheckable(true);
	connect(openCamAct,SIGNAL(triggered()), this, SLOT(OnCamOpen()));

	sobelOperatorAct = new QAction(tr("&Sobel Operator"),this);
	sobelOperatorAct->setCheckable(false);
	connect(sobelOperatorAct,SIGNAL(triggered()),this,SLOT(OnSobelOperatorActClicked()));

	prewittOperatorAct = new QAction(tr("&Prewitt Operator"),this);
	prewittOperatorAct->setCheckable(false);
	connect(prewittOperatorAct,SIGNAL(triggered()),this,SLOT(OnPrewittOperatorClicked()));


	logOperatorAct = new QAction(tr("&LOG Operator"),this);
	logOperatorAct->setCheckable(false);
	connect(logOperatorAct,SIGNAL(triggered()),this,SLOT(OnLogOperatorClicked()));

	cannyDetectionAct = new QAction(tr("&Canny Edge Detection"), this);
	cannyDetectionAct->setCheckable(true);
	connect(cannyDetectionAct,SIGNAL(triggered()), this, SLOT(OnCannyDetectionClicked()));


	rectangleDetectionAct = new QAction(tr("&Detect Rectangle Detection"), this);
	rectangleDetectionAct->setCheckable(true);
	connect(rectangleDetectionAct,SIGNAL(triggered()), this, SLOT(OnRectangleDetectionClicked()));

	pyr_LKAct = new QAction(tr("Optical Flow Pyr_LK"),this);
	pyr_LKAct ->setCheckable(false);
	connect(pyr_LKAct,SIGNAL(triggered()),this, SLOT(OnPyr_LKClicked()));

	staticFaceDetectionAct = new QAction(tr("Face Detection"),this);
	staticFaceDetectionAct ->setCheckable(false);
	connect(staticFaceDetectionAct,SIGNAL(triggered()),this, SLOT(OnStaticFaceDetectionActClicked()));

	videoFaceDetectionAct= new QAction(tr("Face Detection"),this);
	videoFaceDetectionAct ->setCheckable(false);
	connect(videoFaceDetectionAct,SIGNAL(triggered()),this, SLOT(OnVideoFaceDetectionActClicked()));

	drawActGroup = new QActionGroup(this);
	// 	drawActGroup->setExclusive(true);
	drawActGroup->addAction(lineAct);
	drawActGroup->addAction(polylineAct);
	drawActGroup->addAction(rectangleAct);
	drawActGroup->addAction(polygonAct);
	drawActGroup->addAction(arcAct);
	drawActGroup->addAction(pieAct);
	drawActGroup->addAction(chordAct);
	drawActGroup->addAction(ellipseAct);
	drawActGroup->addAction(textAct);
	connect(drawActGroup, SIGNAL(triggered(QAction*)), this, SLOT(draw(QAction*)));	
}

void OCEANUI::CMainWindow::createToolBars()
{
	drawToolBar = addToolBar(tr("Draw"));
	drawToolBar->addAction(lineAct);
	drawToolBar->addAction(polylineAct);
	drawToolBar->addAction(rectangleAct);
	drawToolBar->addAction(polygonAct);
	drawToolBar->addAction(arcAct);
	drawToolBar->addAction(pieAct);
	drawToolBar->addAction(chordAct);
	drawToolBar->addAction(ellipseAct);
	drawToolBar->addSeparator();
	drawToolBar->addAction(textAct);
	//fontCmb = new QFontComboBox(drawToolBar);
	//drawToolBar->addWidget(fontCmb);
}

void OCEANUI::CMainWindow::createMenus()
{

	fileMenu = menuBar()->addMenu(tr("&File"));
	fileMenu->addAction(openAct);
	fileMenu->addAction(openCamAct);

	drawMenu = menuBar()->addMenu(tr("&Draw"));
	drawMenu->addAction(lineAct);
	drawMenu->addAction(polylineAct);
	drawMenu->addAction(rectangleAct);
	drawMenu->addAction(polygonAct);
	drawMenu->addAction(arcAct);
	drawMenu->addAction(pieAct);
	drawMenu->addAction(chordAct);
	drawMenu->addAction(ellipseAct);
	drawMenu->addAction(textAct);

	edgeDetectionMenu = menuBar()->addMenu(tr("Edge Detection"));
	edgeDetectionMenu->addAction(sobelOperatorAct);
	edgeDetectionMenu->addAction(prewittOperatorAct);
	edgeDetectionMenu->addAction(logOperatorAct);
	edgeDetectionMenu->addAction(cannyDetectionAct);

	shapeDetectionMenu = menuBar()->addMenu(tr("Shape Detection"));
	shapeDetectionMenu->addAction(rectangleDetectionAct);

	trackingDetectionMenu = menuBar()->addMenu(tr("Motion Tracking"));
	trackingDetectionMenu->addAction(pyr_LKAct);

	faceDetectionMenu = menuBar()->addMenu(tr("Face Detection"));
	faceDetectionMenu->addAction(staticFaceDetectionAct);
	faceDetectionMenu->addAction(videoFaceDetectionAct);

}

void OCEANUI::CMainWindow::createDockWindows_()
{
	//dock = new QDockWidget(tr("Left Panel"), this);
	//dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
	//pOptionPanel_ShapeDetection_ = new  OCEANUI::CPanel_ShapeDetection(dock);
	//dock->setWidget(pOptionPanel_ShapeDetection_);
	//dock->setMinimumSize(pOptionPanel_ShapeDetection_->size());
	//addDockWidget(Qt::LeftDockWidgetArea, dock);
}

OCEANUI::CCentralCanvasBase* OCEANUI::CMainWindow::setCurrentCentralCanvas_(CentralWidgetType newType)
{
	if (PaintCanvas == newType)
	{
		if ( NULL == CentralCanvasGroup[PaintCanvas])
		{
			form = new CPaintCanvas(this);
			CentralCanvasGroup[CentralWidgetType::PaintCanvas] = form;
		}
	}
	else if (VideoCanvas == newType )
	{
		if ( NULL == CentralCanvasGroup[VideoCanvas])
		{
			form = new CVideoCanvas(this);
			CentralCanvasGroup[VideoCanvas] = form;
		}

	}
	else if (CamCanvas == newType)
	{
		if ( NULL == CentralCanvasGroup[CamCanvas])
		{
			form = new CCamCaptureCavas(this);
			CentralCanvasGroup[CentralWidgetType::CamCanvas] = form;
		}
	}
	setCentralWidget(form);
	return form;
}
void OCEANUI::CMainWindow::draw(QAction* action)
{
    CPaintCanvas* pPC = dynamic_cast<CPaintCanvas*>(form);
	if ( NULL ==pPC)
	{
		return;
	}
	
	if(action == lineAct)
		pPC->setShape(CPaintCanvas::Line);
	else if(action == polylineAct)
		pPC->setShape(CPaintCanvas::Polyline);
	else if(action == rectangleAct)
		pPC->setShape(CPaintCanvas::Rectangle);
	else if(action == polygonAct)
		pPC->setShape(CPaintCanvas::Polygon);
	else if(action == arcAct)
		pPC->setShape(CPaintCanvas::Arc);
	else if(action == pieAct)
		pPC->setShape(CPaintCanvas::Pie);
	else if(action == chordAct)
		pPC->setShape(CPaintCanvas::Chord);
	else if(action == ellipseAct)
		pPC->setShape(CPaintCanvas::Ellipse);
	else if((action == textAct))
		pPC->setShape(CPaintCanvas::Text);
}
void OCEANUI::CMainWindow::OnImageChanged( IplImage* pImage )
{
   

}


void   OCEANUI::CMainWindow:: OnFileOpen()
{
	QStringList filters;
	filters << "Image files (*.bmp *.jpg)"
		<< "Any files (*)";

	QFileDialog dialog(this);
	dialog.setNameFilters(filters);
	QString qFileName =  dialog.getOpenFileName();
     QFileInfo qFileInfo(qFileName);
	QString ext = qFileInfo.completeSuffix();
	void* pData = NULL;
	if (ext == "bmp" || ext == "jpg")
	{
		pData = cvLoadImage(qFileName.toLocal8Bit().data());
		if ( NULL == pData)
		{
			std::cout << "Image open failed" << std::endl;
			return;
		}
		setCurrentCentralCanvas_(PaintCanvas);
		pCurrentImage_ = reinterpret_cast<IplImage*>(pData);
	}
	else if (ext == "avi")
	{
		pData = cvCaptureFromAVI(qFileName.toLocal8Bit().data());
		if ( NULL == pData)
		{
			std::cout << "Video open failed" <<std::endl;
			return;
		}
		setCurrentCentralCanvas_(VideoCanvas);
		pCurrentImage_ = NULL;
	}
	form ->initData(pData);
	
}



void OCEANUI::CMainWindow::OnCamOpen()
{
	setCurrentCentralCanvas_(CamCanvas);
	form->initData(NULL);

}

void OCEANUI::CMainWindow::OnSobelOperatorActClicked()
{
	
	CPanelBase* pPanel =  pPanelManager->AddPanel("OptionPanel_EdgeDetection");
	pPanel->setContext("SobelOperator");
	pPanel->setImage(pCurrentImage_);
	pPanelManager->ShowPanel(pPanel);
	
}


void OCEANUI::CMainWindow::OnPrewittOperatorClicked()
{
	CPanelBase* pPanel =  pPanelManager->AddPanel("OptionPanel_EdgeDetection");
	pPanel->setContext("PrewittOperator");
	pPanel->setImage(pCurrentImage_);
	pPanelManager->ShowPanel(pPanel);
}

void OCEANUI::CMainWindow::OnLogOperatorClicked()
{
	CPanelBase* pPanel =  pPanelManager->AddPanel("OptionPanel_EdgeDetection");
	pPanel->setContext("LogOperator");
	pPanel->setImage(pCurrentImage_);
	pPanelManager->ShowPanel(pPanel);
}

void OCEANUI::CMainWindow::OnCannyDetectionClicked()
{

	 
	CPanelBase* pPanel =  pPanelManager->AddPanel("OptionPanel_EdgeDetection");
	pPanel->setContext("CannyOperator");
	pPanel->setImage(pCurrentImage_);
	pPanelManager->ShowPanel(pPanel);


}

void OCEANUI::CMainWindow::OnRectangleDetectionClicked()
{

	CPanelBase* pPanel =  pPanelManager->AddPanel("OptionPanel_ShapeDetection");
	pPanel->setImage(pCurrentImage_);
	pPanelManager->ShowPanel(pPanel);
		 
}

void OCEANUI::CMainWindow::OnPyr_LKClicked()
{
	CPanelBase* pPanel =  pPanelManager->AddPanel("OptionPanel_Pyr_LK");
	pPanelManager->ShowPanel(pPanel);
}

void OCEANUI::CMainWindow::OnStaticFaceDetectionActClicked()
{
	CPanelBase* pPanel =  pPanelManager->AddPanel("OptionPanel_FaceDetection");
	pPanel->setImage(pCurrentImage_);
	pPanel->setContext("Static Face");
	pPanelManager->ShowPanel(pPanel);
}

void OCEANUI::CMainWindow::OnVideoFaceDetectionActClicked()
{
	CPanelBase* pPanel =  pPanelManager->AddPanel("OptionPanel_FaceDetection");
	pPanel->setImage(pCurrentImage_);
	pPanel->setContext("Video Face");
	pPanelManager->ShowPanel(pPanel);
}